package com.lv.service.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lv.service.R;
import com.lv.service.webview.ConsealWebActivity;

/**
 * 开始的对话弹窗
 */

public class FirstDialog extends Dialog {

    private TextView tvDsLeft;
    private TextView tvDsRight;
    private TextView tvClick;
    private Context mContext;
    private DisplayMetrics mDisplayMetrics;
    private float mWidthScale = 0.7f;
    private View.OnClickListener mOnLeftClickListener;
    private View.OnClickListener mOnRightClickListener;

    public FirstDialog(Context context) {
        super(context, com.lv.common.R.style.TipDialogStyle);
        this.mContext = context;
    }

    public FirstDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected FirstDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mDisplayMetrics = mContext.getResources().getDisplayMetrics();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_first, null);
        tvDsLeft = view.findViewById(R.id.tv_ds_left);
        tvDsRight = view.findViewById(R.id.tv_ds_right);
        tvClick = view.findViewById(R.id.tv_click);

        int width = (int) (mDisplayMetrics.widthPixels * mWidthScale);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(view, layoutParams);
        setLeftBtnClickListener(mOnLeftClickListener);
        setRightBtnClickListener(mOnRightClickListener);
        SpannableStringBuilder spannable = new SpannableStringBuilder("你可阅读《用户协议》和《隐私政策》了解详细信息。如你同意，请点击同意开始接受我们的服务。");
        //设置文字的前景色
        spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#6585B1")), 4, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#6585B1")), 11, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //这个一定要记得设置，不然点击不生效
        tvClick.setMovementMethod(LinkMovementMethod.getInstance());
        spannable.setSpan(new TextClickOne(), 4, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new TextClickTwo(), 11, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvClick.setText(spannable);

    }

    private class TextClickOne extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            //在此处理点击事件
            tvClick.setBackgroundColor(mContext.getResources().getColor(com.lv.common.R.color.color_10));
            ConsealWebActivity.Companion.openUrl(mContext,"5");
        }

        @Override
        public void updateDrawState(TextPaint ds) {

        }
    }

    private class TextClickTwo extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            //在此处理点击事件
            tvClick.setBackgroundColor(mContext.getResources().getColor(com.lv.common.R.color.color_10));
            ConsealWebActivity.Companion.openUrl(mContext,"0");
        }

        @Override
        public void updateDrawState(TextPaint ds) {

        }
    }

    public FirstDialog setLeftBtnClickListener(View.OnClickListener listener) {
        if (listener != null && tvDsLeft != null) {//dialog未调用show时控件为null
            tvDsLeft.setOnClickListener(listener);
        } else {
            this.mOnLeftClickListener = listener;
        }
        return this;
    }

    public FirstDialog setRightBtnClickListener(View.OnClickListener listener) {
        if (listener != null && tvDsRight != null) {
            tvDsRight.setOnClickListener(listener);
        } else {
            this.mOnRightClickListener = listener;
        }
        return this;
    }


}
